package cn.skyquant.quant4j.api.authority;

import com.google.common.base.MoreObjects;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * 用户
 * Created by harley-dog on 2016/5/12.
 */
public class UserDTO implements Serializable{
    private static final long serialVersionUID = 1L;
    public Integer id;
    public String name;
    public String tel;
    public String token;
    public Date gmtLastLogin;
    private final Set<String> roles = new HashSet<String>();
    public void addRole(String roleName){
        roles.add(roleName);
    }
    public boolean containRole(String roleName){
        return roles.contains(roleName);
    }
    public boolean containAny(String[] roleNames){
        for(String roleName:roleNames){
            if(roles.contains(roleName)) return true;
        }
        return false;
    }
    public String getRoleStr(){return roles.toString();}
    public Set<String> getRoles(){return roles;}

    public UserDTO(){
    }
    public UserDTO(String tel,String name,String ...roles){
        this.tel = tel;
        this.name = name;
        if(roles!=null){
            for(String role:roles){
                this.addRole(role);
            }
        }
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        UserDTO user = (UserDTO) o;

        return id != null ? id.equals(user.id) : user.id == null;
    }

    @Override
    public int hashCode() {
        return id != null ? id.hashCode() : 0;
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this)
                .add("id", id)
                .add("name", name)
                .add("tel", tel)
                .toString();
    }
}
